<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/28
 * Time: 14:21
 */

namespace app\admin\controller;

use app\admin\model\AdminModel;
use app\admin\model\RoleModel;
use app\admin\model\RuleModel;
use think\Request;
use think\Config;

class Admin extends Base
{
    protected $adminModel;
    protected $ruleModel;
    protected $roleModel;

    /*
    * 依赖注入Model
    * */
    public function __construct(AdminModel $adminModel,RoleModel $roleModel,RuleModel $ruleModel){
        parent::__construct();
        $this->adminModel=$adminModel;
        $this->roleModel = $roleModel;
        $this->ruleModel = $ruleModel;
    }

    /*
     * 登录
     * */
    public function login(){
        if (Request::instance()->isPost()){
            $username=input("post.username/s");
            $password=input("post.password/s");
            $rember=input('post.rember/s');
            $map["username"]=$username;
            $map["password"]=md5($password);
            $map["status"]=1;
            $data=$this->adminModel->getByMaps($map);
            if(count($data)==1){
                //如果该用户角色为禁用状态则禁止登录
                if($this->roleModel->isOn($data['role_id'])){
                    $role_rule=$this->ruleModel->getByRoleId($data['role_id']);
                    foreach($role_rule as $value){
                        if(empty($_SESSION["authority"][$value["controller"]])){
                            $_SESSION["authority"][$value["controller"]]=[];
                        }
                        array_push($_SESSION["authority"][$value["controller"]],$value["action"]);
                    }unset($value);
                    $_SESSION["user_id"]=$data["id"];
                    $_SESSION["role_id"]=$data["role_id"];
                    $_SESSION["username"]=$data["username"];
                    $_SESSION["name"]=$data["name"];
                    if ($rember=='true'){
                        setcookie("username", $map["username"], time() + 3600*24*2);
                        setcookie("password", $password, time() + 3600*24*2);
                        setcookie("rember", true, time() + 3600*24*2);
                    }else{
                        setcookie("username", "", time() - 3600);
                        setcookie("password", "", time() - 3600);
                        setcookie("rember", false, time() - 3600);
                    }
                    $params=[
                        Config::get('op_type')[4],
                        Config::get('op_module')[2],
                        Config::get('op_text')[3],
                        $data['username']
                    ];
                    tag('logs_db',$params);
                    $this->success('登录成功');
                }
                $this->error("该账户权限被禁用",'admin/login');
            }
            else{
                $this->error("账号或密码错误/账户被禁用",'admin/login');
            }
        }

        $this->assign("username",isset($_COOKIE["username"])?$_COOKIE["username"]:'');
        $this->assign("password",isset($_COOKIE["password"])?$_COOKIE["password"]:'');
        $this->assign("rember",isset($_COOKIE["rember"])?$_COOKIE["rember"]:'');
        return $this->fetch('admin/login');
    }

    /*
    * 登出
    * */
    public function logout(){
        session_unset();
        session_destroy();
        $this->redirect('login');
    }

    /*
    * 后台首页
    * */
    public function main(){
        if(!isset($_SESSION["username"])) {
            $this->redirect('login');
        }else{
            if (isset($_SESSION["authority"])){
                $data=$_SESSION["authority"];
                $this->assign("data",$data);
            }
        }
        return $this->fetch('main/index');
    }

    /*
    * 控制台
    * */
    public function controller(){
        return $this->fetch('main/controller');
    }

    /*
     * 个人信息
     * */
    public function profile(){
        $id=$_SESSION["user_id"];
        $this->assign('roles',$this->roleModel->listAll());
        $this->assign("data",$this->adminModel->getById($id));
        return $this->fetch('main/profile');
    }

    /*
     * 管理员列表
     * */
    public function admin_index(){
        //角色列表
        $this->assign('roles',$this->roleModel->listAll());
        $this->assign("data",$this->adminModel->listAll());
        return $this->fetch('admin/index');
    }

    /*
    * 新增管理员
    * */
    public function admin_add(){
        if(Request::instance()->isPost()){
            $validate=$this->adminModel->validate_model('add');
            if ($validate['code']){
                return $this->adminModel->add();
            }else{
                return $validate;
            }
        }
        $this->assign('roles',$this->roleModel->listAll());
        return $this->fetch('admin/add');
    }

    /*
    * 修改管理员
    * */
    public function admin_edit(){
        if(Request::instance()->isPost()){
            $validate=$this->adminModel->validate_model('edit');
            if ($validate['code']){
                return $this->adminModel->edit();
            }else{
                return $validate;
            }
        }
        $id=input('get.id/d');
        $this->assign('roles',$this->roleModel->listAll());
        $this->assign("data",$this->adminModel->getById($id));
        return $this->fetch('admin/edit');
    }

    /*
    * 删除管理员
    * */
    public function admin_del(){
        return $this->adminModel->del();
    }

}